package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessPest;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.PestVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.SearchPestHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/4/1 16:57
 * 修改备注：
 * ================================================
 */
public class PestSearchListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {


    @Bind(R.id.pest_search_connection_expert)
    RelativeLayout pestSearchConnectionExpert;
    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;


    private RecyclerArrayAdapter adapter;
    private List<PestVO> pestVOList = new ArrayList<>();
    private String typeCode, feature;

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_pest_search_list);
        setTitle("诊断结果");
    }

    @Override
    public void initMember() {

        Map<String, String> map = (Map<String, String>) getIntent().getSerializableExtra("map");
        if (map == null)
            return;

        typeCode = map.get("typeCode");
        feature = map.get("feature");

        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.addItemDecoration(Utils.setSpaceDecoration(this, false, true, false));

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new SearchPestHolder(parent);
            }
        });

        onRefresh();

    }

    @Override
    public void addListener() {
        recyclerView.setRefreshListener(this);

        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                PestVO pestVO = (PestVO) adapter.getAllData().get(position);
                GotoUtil.gotoActivity(PestSearchListActivity.this, PestDiagnosisDetailActivity.class, "pestVO", pestVO);
            }
        });


        pestSearchConnectionExpert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GotoUtil.gotoActivity(PestSearchListActivity.this, ExpertConsultationActivity.class);
            }
        });
    }

    @Override
    public void setHandler() {

        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                if (recyclerView == null) {
                    return;
                }
                if (recyclerView.getSwipeToRefresh().isRefreshing())
                    recyclerView.setRefreshing(false);

                switch (msg.what) {
                    case MSG.MSG_GET_DIAGNOSIS_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_DIAGNOSIS_FIELD:
                        if (adapter.getAllData().size() > 0)
                            adapter.pauseMore();
                        else
                            recyclerView.showEmpty();
                        break;
                }
            }
        };

    }

    @Override
    public void callBack(Object object) {

        pestVOList = FastJsonUtil.getListBean(object.toString(), "body", "resultList", PestVO.class);
        adapter.clear();

        adapter.addAll(pestVOList);

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    public void onRefresh() {

        BusinessPest.getDiagnosisList(this, typeCode, feature, mHandler);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
